﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.Entidades;
using RN.Componentes;

namespace Sitio
{
    public partial class WebFormActivos : System.Web.UI.Page
    {
        private string[] tablaAnsi = {   " ", "!", "''", "#", "$", "%", "&", " ", "(", ")", "*", "+",",","-", ".", "/"
                                         , "0","1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">","?", 
                                         "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P","Q", 
                                         "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[","/", "]", 
                                         "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "q", "r", 
                                         "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", " ", "€", " ", "‚", "ƒ", "„", "…", "†", 
                                         "‡", "ˆ", "‰", "Š", "‹", "Œ", " ", "Ž", " ", " ", "‘", "’", "“", "”", "•", "–", "—", "˜", "™", "š",
                                         "›œ", " ", "ž", "Ÿ", " ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", " ", "®", "¯", 
                                         "°", "±", "²", " ", "³", "´", "µ", "¶", "•", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", 
                                         "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", " ", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", 
                                         "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", " ", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", 
                                         "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", " ", "û", 
                                         "ü", "ý", "þ", "ÿ" };

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this.Session["usuario"] == null)
                {
                    Response.Redirect("Login.aspx");
                }
                
                Cargar_grid(); List<RN.Entidades.Familias> f = CFamilia.Traer();
            ddlFamilia.DataSource = f;
            ddlFamilia.DataTextField = "SNOMBRE1";
            ddlFamilia.DataValueField = "LFAMILIA_ID1";
            ddlFamilia.DataBind();
            List<RN.Entidades.Personal> p = CPersonal.Traer();
            ddlPersonal.DataSource = p;
            ddlPersonal.DataTextField = "Snombre";
            ddlPersonal.DataValueField = "LPERSONAL_ID1";
            ddlPersonal.DataBind();
            List<RN.Entidades.Proveedor> pr = CProveedor.Traer();
            ddlProveedor.DataSource = pr;
            ddlProveedor.DataTextField = "SNOMBRE1";
            ddlProveedor.DataValueField = "LPROVEEDOR1";
            ddlProveedor.DataBind();
            DropDownList1.Items.Add("Propio");
            DropDownList1.Items.Add("Tercero");
            DropDownList2.Items.Add("Custodia");
            DropDownList2.Items.Add("Disponible");
            //DropDownList2.Items.Add("Responsable");
            DropDownList3.Items.Add("Excelente");
            DropDownList3.Items.Add("bueno");
            DropDownList3.Items.Add("regular");
            DropDownList3.Items.Add("malo");
            DropDownList4.Items.Add("Compra");
            DropDownList4.Items.Add("Donación");
            DropDownList4.Items.Add("Custodia");
            DropDownList5.Items.Add("Si");
            DropDownList5.Items.Add("No");
            }
           
        }

        public void Cargar_grid()
        {
            List<Activos> obj = CActivos.TraerACTIVO();
            grdActivos.DataSource = obj;
            grdActivos.DataBind();
        }

        protected void btRegresar_Click(object sender, EventArgs e)
        {
            Response.Redirect("./Principal.aspx");
        }

        protected void btGuardar_Click(object sender, EventArgs e)
        {

            Activos a = new Activos();
            a.SDESCIPCION_DSC1 = txbDescripcion.Text;
            a.SPROPIEDAD1 = DropDownList1.SelectedItem.ToString();
            a.DTFECHACOMPRA_DT1 = CalendarFechaCompra.SelectedDate;
            a.DTFECHADEALTA_DT1 = DateTime.Now;
            //a.IVIDAUTIL1 = Convert.ToInt32(txbVidaUtil);
            a.ICODIGOBARRAS1 = GenerarCodigo();
            a.ICODIGOBARRAS1 = GenerarEnBarCode128(a.ICODIGOBARRAS1);
            a.SESTADO1 = DropDownList3.SelectedItem.ToString();
            a.SMARCA1 = txbMarca.Text;
            a.SMODELO1 = txbModelo.Text;
            a.SFORMA1 = txbForma.Text;
            a.SSERIAL1 = txbSerial.Text;
            a.SCOLOR1 = txbColor.Text;
            a.SCAPACIDAD1 = txbCapacidad.Text;
            a.SMATERIAL1 = txbMaterial.Text;
            a.SALTO1 = txbAlto.Text;
            a.SLARGO1 = txbLargo.Text;
            a.SANCHO1 = txbAncho.Text;
            a.IDIAMETRO1 = Convert.ToInt32(txbDiametro.Text);
            a.INUMERODEDIVICIONES1 = Convert.ToInt32(txbNumeroDivisiones.Text);
            a.STIPODEASIGANCION1 = DropDownList2.SelectedItem.ToString();
            a.STIPODEALTA1 = DropDownList4.SelectedItem.ToString();
            a.NUMERODECOMPROVANTE1 = Convert.ToInt32(txbNumeroComprobante.Text);
            a.REPORTADOALSEGURO1 = DropDownList5.SelectedItem.ToString();
            a.IVALOR1 = Convert.ToDouble(txbValor.Text);
            a.LPROVEEDORE_ID1.LPROVEEDOR1 = Convert.ToInt32(ddlProveedor.SelectedValue);
            a.LPERSONAL_ID1.LPERSONAL_ID1 = Convert.ToInt32(ddlPersonal.SelectedValue);
            a.LFAMILIAS_ID1.LFAMILIA_ID1 = Convert.ToInt32(ddlFamilia.SelectedValue);

            CActivos.Insertar(a);

            Cargar_grid();
        }
        protected void grdActivos_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlProveedor_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        public string GenerarCodigo()
        {
            string codigo = "";
            string Ultimo = (CActivos.TraerUltimoCreado()).LACTIVOS_ID1 + "";
            if (Ultimo.Length == 1)
            {
                codigo = "41477700000000" + Ultimo;
            }
            if (Ultimo.Length == 2)
            {
                codigo = "4147770000000" + Ultimo;
            }
            if (Ultimo.Length == 3)
            {
                codigo = "414777000000" + Ultimo;
            }
            if (Ultimo.Length == 4)
            {
                codigo = "41477700000" + Ultimo;
            }
            if (Ultimo.Length == 5)
            {
                codigo = "4147770000" + Ultimo;
            }
            if (Ultimo.Length == 6)
            {
                codigo = "414777000" + Ultimo;
            }
            if (Ultimo.Length == 7)
            {
                codigo = "41477700" + Ultimo;
            }
            if (Ultimo.Length == 8)
            {
                codigo = "4147770" + Ultimo;
            }
            if (Ultimo.Length == 9)
            {
                codigo = "414777" + Ultimo;
            }
            int pos = 1;
            int total = 0;
            for (int i = codigo.Length - 12; i < codigo.Length; i++)
            {
                int numero = Convert.ToInt16(codigo.Substring(i, 1));
                if (pos % 2 == 0)
                {
                    total = total + numero * 3;
                }
                else
                {
                    total = total + numero * 1;
                }
                pos++;
            }
            codigo = codigo + total;
            int funcion = Convert.ToInt32(codigo.Substring(15));
            Ultimo = codigo.Substring(15);
            Ultimo = Ultimo.Substring(0, 1);
            total = (Convert.ToInt32(Ultimo + "0") + 10) - funcion;
            codigo = codigo + total;
            return codigo;
        }
        public string GenerarEnBarCode128(string codigo1)
        {
            string codigo = "‰";
            int secuencia = 1;
            for (int i = 0; i < codigo1.Length; i++)
            {
                int valor = Convert.ToInt32(codigo1.Substring(i, 2));
                valor = valor + 32;
                i++;
                if (tablaAnsi[valor - 32].Equals(" "))
                {
                    codigo = codigo + "è";
                }
                else
                {
                    codigo = codigo + tablaAnsi[valor - 32];
                }
            }
            int total = 0;
            for (int i = 0; i < codigo1.Length; i++)
            {
                int valor = Convert.ToInt32(codigo1.Substring(i, 2));
                total = (Convert.ToInt32(codigo1.Substring(i, 2)) * secuencia) + total;
                total = total % 103;
                i++;
                secuencia++;
            }
            codigo = codigo + tablaAnsi[total] + "Š";
            return codigo;
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btNuevo_Click(object sender, EventArgs e)
        {

        }

    }

}
